"""
Use this script to convert json source in the CouchDB version to bibjson format
"""

import json
import re

def to_ascii(ss):
    return ''.join(cc for cc in ss if ord(cc) < 128)

def to_id(ss):
    return re.sub(r'\s+', '_', to_ascii(ss).lower()).replace('.', '')

def handle_topic(doc):
    doc['id'] = to_id(doc['name'])
    doc['title'] = doc['name']
    del doc['categories']

def handle_person(doc):
    doc['firstname'] = doc['first_name']
    doc['lastname'] = doc['last_name']
    doc['name'] = doc['firstname'] + ' ' + doc['lastname']
    doc['title'] = doc['name']
    doc['id'] = to_id(doc['name'])
    if not 'knowledge' in doc:
        doc['knowledge'] = []
    for ii, kk in enumerate(doc['knowledge']):
        doc['knowledge'][ii]['id'] = to_id(kk['topic'].split('/', 1)[-1])
        del doc['knowledge'][ii]['topic']
    del doc['first_name']
    del doc['last_name']
    if 'sources' in doc:
        del doc['sources']

def handle_publisher(doc):
    doc['id'] = to_id(doc['name'])
    doc['url'] = doc['website']
    doc['title'] = doc['name']
    del doc['website']

def handle_course(doc):
    doc['id'] = to_id(doc['name'] + ' ' + doc['semester'])
    doc['title'] = doc['name'] + ' ' + doc['semester']
    doc['book'] = [ee for ee in doc['books']] 
    for ii, book_name in enumerate(doc['books']):
        doc['book'][ii] = {'id': to_id(book_name.split('/', 1)[-1])} 
    doc['lecturer'] = [ee for ee in doc['lecturers']]
    for ii, person_name in enumerate(doc['lecturers']):
        doc['lecturer'][ii] = {'id': to_id(person_name.split('/', 1)[-1]), 
                            'name': person_name.split('/', 1)[-1].replace('_', ' ')}
    doc['lecture'] = [ee for ee in doc['lectures']]
    for ii, lecture in enumerate(doc['lectures']):
        doc['lecture'][ii]['topic'] = [{ 'id': to_id(topic.split('/', 1)[-1]) }
                                            for topic in lecture['topics']]
        del doc['lecture'][ii]['doc_type']
        del doc['lecture'][ii]['topics']
    #use singular name
    del doc['lecturers']
    del doc['books'] 
    del doc['lectures']

def handle_book(doc):
    def process_toc(toc_entry):
        toc_entry['topic'] = [{ 'id': to_id( topic.split('/', 1)[-1] ) }
                                    for topic in toc_entry['topics']]
        toc_entry['child'] = [process_toc(child) for child in toc_entry['children']]
        del toc_entry['doc_type']      
        del toc_entry['topics'] #use singular name
        del toc_entry['children'] #use singular name
        return toc_entry
    
    doc['id'] = to_id(doc['title'] + '_1_ed') if \
                    len(doc.get('edition', '')) == 0 else \
                    to_id(doc['title'] + ' ' + doc['edition'] + '_ed')
    doc['publisher'] = {'id': to_id(doc['publisher'].split('/', 1)[-1])}
    doc['url'] = doc['publisher_listing_url']
    doc['author'] = [ee for ee in doc['authors']]
    for ii, person_name in enumerate(doc['authors']):
        doc['author'][ii] = {'id': to_id(person_name.split('/', 1)[-1]), 
                            'name': person_name.split('/', 1)[-1].replace('_', ' ')}
    doc['toc'] = [process_toc(toc_entry) for toc_entry in doc['toc']]
    del doc['publisher_listing_url']
    del doc['authors']


couchdb_docs = json.load(open('./sti_couchdb_dump.JSON','r'))
couchdb_docs = [ee['doc'] for ee in couchdb_docs['rows']]
bib_docs = []
tags_to_remove = 'doc_type, _id, _rev'.split(', ')
for doc in couchdb_docs:
    if not 'doc_type' in doc: # design doc
        continue
    doc['type'] = doc['doc_type'].lower()
    if doc['type'] == 'topic':
        handle_topic(doc)
    if doc['type'] == 'person':
        handle_person(doc)
    if doc['type'] == 'publisher':
        handle_publisher(doc)
    if doc['type'] == 'course':
        handle_course(doc)
    if doc['type'] == 'book':
        handle_book(doc)
    for tag in tags_to_remove:
        del doc[tag]
    bib_docs.append(doc)

json.dump(bib_docs, open('./bib_dump.json', 'w'), indent=4)






